/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Chassis.java
 *
 * Created on Oct 27, 2010, 7:53:26 PM
 */

package lmclogistics;


import components.Dates;
import components.Text;
import components.TextArea;
import components.Image;
import components.NButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import sql.Sql;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Sonia Tan
 */
public class AddChassis extends javax.swing.JFrame {

    /** Creates new form Chassis */


    private Image bg;

    ButtonGroup sizeB = new ButtonGroup();

        final JRadioButton size20 = new JRadioButton();
        final JRadioButton size40 = new JRadioButton();
        final JRadioButton sizec = new JRadioButton();

        TextArea prefixField = new TextArea();
        TextArea serialNoField = new TextArea();
        TextArea plateNoField = new TextArea();
        Dates registryDateField = new Dates();
        Dates expiryDateField = new Dates();
        TextArea orNoField = new TextArea();
        TextArea crNoField = new TextArea();

        ButtonGroup cebuB = new ButtonGroup();

        final JRadioButton cebuY = new JRadioButton();
        final JRadioButton cebuN = new JRadioButton();

        ButtonGroup damage = new ButtonGroup();

        final JRadioButton damageY = new JRadioButton();
        final JRadioButton damageN = new JRadioButton();

    public AddChassis() {
        setLayout(null);
        setTitle("Add Chassis");
        setAlwaysOnTop(true);
        setSize(512, 488);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        centerScreen();
        setVisible(true);

        setBg();
        okCancelButtons();
        initText();
        initFlds();        

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public final void setBg(){
        bg = new Image("panel.png",509,460);
        bg.setLocation(0,0);

        add(bg,-1);
    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void okCancelButtons()
    {
        NButton cancel = new NButton("Cancel",410,420);
        NButton ok = new NButton("Ok",320 ,420);
        add(ok,0);
        add(cancel,0);
        cancel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                        dispose();
                    }
                });
        ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    //FormWarning warn = new FormWarning("incorrect");
                    addChassis();
                    }
                });
    }

    public void addChassis()
    {   String cSize = "20", cCebu = "1", cDamage = "1";

        if(size20.isSelected()==true)
            cSize = "20";
        if(size40.isSelected()==true)
            cSize = "40";
        if(sizec.isSelected()==true)
            cSize = "Combo";

        if(cebuY.isSelected()==true)
            cCebu = "1";
        if(cebuN.isSelected()==true)
            cCebu = "0";

        if(damageY.isSelected()==true)
            cDamage = "1";
        if(damageN.isSelected()==true)
            cDamage = "0";


        try {
                String query = "INSERT INTO chassis(chsSize, chsPrefix, chsSrlNo, chsPltNo, chsRegDate, chsExpDate, chsORNo, chsCRNo, chsCebu, chsDamage, deleted) " +
                " VALUES('"+cSize+"','"+prefixField.getText()+"','"+serialNoField.getText()+"','"+plateNoField.getText()+"','"+registryDateField.getDate()+"','"+expiryDateField.getDate()+"','"+orNoField.getText()+"','"+crNoField.getText()+"','"+cCebu+"','"+cDamage+"',0)";


                Connection connection = Sql.getSqlConnection();
                Statement stmt = connection.createStatement();
                stmt.executeUpdate(query);


                Sql.closeSqlConnection(connection);

                } catch (Exception ex) {
                Logger.getLogger(AddChassis.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.dispose();

    }

   
    public void initText(){
        Text code = new Text();
        Text cd = new Text();
        Text size = new Text();
        Text prefix = new Text();
        Text serialNo = new Text();
        Text plateNo = new Text();
        Text registryDate = new Text();
        Text expiryDate = new Text();
        Text orNo = new Text();
        Text crNo = new Text();
        Text cebu = new Text();
        Text damage = new Text();

        code.inText("Code:", 15, 15);        
        size.inText("Size:", 15, 40);
        prefix.inText("Prefix:", 15, 65);
        serialNo.inText("Serial No.:", 15 , 90);
        plateNo.inText("Plate No.:", 15, 115);
        registryDate.inText("Registry Date:", 15, 140);
        expiryDate.inText("Expiry Date:", 15, 165);
        orNo.inText("OR No.:", 15, 190);
        crNo.inText("CR No.:", 15, 215);
        cebu.inText("CEBU", 15, 240);
        damage.inText("DAMAGE", 15, 265);

        add(code,0);
        add(cd,0);
        add(size,0);
        add(prefix,0);
        add(serialNo,0);
        add(plateNo,0);
        add(registryDate,0);
        add(expiryDate,0);
        add(orNo,0);
        add(crNo,0);
        add(cebu,0);
        add(damage,0);

        String codeVal = "SELECT MAX(chsPin) FROM chassis";

        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(codeVal);

            if(rs.next())
            {
                cd.inText(""+(Integer.parseInt(rs.getString(1))+1),100,15);
            }
        } catch(Exception ex)
        {
            Logger.getLogger(AddChassis.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    /*
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        getContentPane().setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
*/
    private void initFlds() {


        sizeB.add(size20);
        sizeB.add(size40);
        sizeB.add(sizec);


        cebuB.add(cebuY);
        cebuB.add(cebuN);



        

        damage.add(damageY);
        damage.add(damageN);
      
        prefixField.initText(60, 25, 100, 65);
        serialNoField.initText(60, 25, 100 , 90);
        plateNoField.initText(60, 25, 100, 115);
        registryDateField.initDates(350, 25, 120, 140);
        expiryDateField.initDates(350, 25, 120, 165);
        orNoField.initText(60, 25, 100, 190);
        crNoField.initText(60, 25, 100, 215);

        size20.setText("20");
        size40.setText("40");
        sizec.setText("c");

        cebuY.setText("Yes");
        cebuN.setText("No");

        damageY.setText("Yes");
        damageN.setText("No");

        size20.setLocation(100,39);
        size40.setLocation(150,39);
        sizec.setLocation(200,39);
        size20.setSize(50,25);
        size40.setSize(50,25);
        sizec.setSize(50,25);

        cebuY.setLocation(100,240);
        cebuN.setLocation(150,240);
        cebuY.setSize(50,25);
        cebuN.setSize(50,25);

        damageY.setSize(50,25);
        damageN.setSize(50,25);
        damageY.setLocation(100,265);
        damageN.setLocation(150,265);
        
        add(size20,0);
        add(size40,0);
        add(sizec,0);
        add(prefixField,0);
        add(serialNoField,0);
        add(plateNoField,0);
        add(registryDateField,0);
        add(expiryDateField,0);
        add(orNoField,0);
        add(crNoField,0);
        add(cebuY,0);
        add(cebuN,0);
        add(damageY,0);
        add(damageN,0);
    }


    public void centerScreen(){

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = getSize().width;
        int h = getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the window
        setLocation(x, y);
    }

      

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
